package com.xhtt.modules.cfg.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.Query;
import com.xhtt.modules.cfg.dao.CfgBerthInfoMapper;
import com.xhtt.modules.cfg.entity.CfgBerthInfo;
import com.xhtt.modules.cfg.entity.CfgBerthRelate;
import com.xhtt.modules.cfg.entity.CfgPotInfo;
import com.xhtt.modules.cfg.entity.CfgPumpInfo;
import com.xhtt.modules.cfg.model.RelateObj;
import com.xhtt.modules.cfg.service.CfgBerthInfoService;
import com.xhtt.modules.cfg.service.CfgBerthRelateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 泊位 服务实现类
 * </p>
 *
 * @author feipc
 * @since 2019-02-28
 */
@Service
public class CfgBerthInfoServiceImpl extends ServiceImpl<CfgBerthInfoMapper, CfgBerthInfo> implements CfgBerthInfoService {

    @Autowired
    private CfgBerthRelateService berthRelateService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<CfgBerthInfo> page = new Query<CfgBerthInfo>(params).getPage();
        List<CfgBerthInfo> list = baseMapper.queryPage(page, params);
        //连接点
//        for (CfgBerthInfo berthInfo : list) {
//            List<CfgBerthRelate> berthRelates = berthRelateService.list(new LambdaQueryWrapper<CfgBerthRelate>().eq(CfgBerthRelate::getBerthId, berthInfo.getId()));
//            List<CfgBerthRelate> guan = berthRelates.stream().filter(cfgBerthRelate -> cfgBerthRelate.getType().intValue() == 1).collect(Collectors.toList());//罐
//            List<CfgBerthRelate> yingguan = berthRelates.stream().filter(cfgBerthRelate -> cfgBerthRelate.getType().intValue() == 2).collect(Collectors.toList());//硬管
//            List<CfgBerthRelate> beng = berthRelates.stream().filter(cfgBerthRelate -> cfgBerthRelate.getType().intValue() == 3).collect(Collectors.toList());//泵
//            List<CfgBerthRelate> matou = berthRelates.stream().filter(cfgBerthRelate -> cfgBerthRelate.getType().intValue() == 4).collect(Collectors.toList());//码
//            berthInfo.setRelateObject("待完善连接点 todo");
//        }

        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public void addBerth(CfgBerthInfo cfgBerthInfo) {
        String numberNo = cfgBerthInfo.getNumberNo();
        int count = this.count(new LambdaQueryWrapper<CfgBerthInfo>().eq(CfgBerthInfo::getNumberNo, numberNo).ne(CfgBerthInfo::getStatus, -1));
        if (count > 0) {
            throw new RRException("编号已存在");
        }
        this.save(cfgBerthInfo);
    }

    @Override
    public void updateBerth(CfgBerthInfo cfgBerthInfo) {
        int count = this.count(new LambdaQueryWrapper<CfgBerthInfo>().eq(CfgBerthInfo::getNumberNo, cfgBerthInfo.getNumberNo()).ne(CfgBerthInfo::getId, cfgBerthInfo.getId()).ne(CfgBerthInfo::getStatus, -1));
        if (count > 0) {
            throw new RRException("编号已存在");
        }
        this.updateById(cfgBerthInfo);
    }

    @Override
    public List<CfgBerthRelate> getRelateObject(Integer berthId) {
        List<CfgBerthRelate> list = berthRelateService.list(new LambdaQueryWrapper<CfgBerthRelate>().eq(CfgBerthRelate::getBerthId, berthId));
        List<RelateObj> relateObjs;
        RelateObj ro;
        for (CfgBerthRelate re : list) {
            relateObjs = new ArrayList<>();
            ro = new RelateObj(re.getRelateId(), re.getName());
            relateObjs.add(ro);
            re.setSubRelates(relateObjs);
        }
        return list;
    }

    @Override
    @Transactional
    public void updateRelateObject(List<CfgBerthRelate> relates) {
        //全删全增
        Integer berthId = relates.get(0).getBerthId();
        if (null == berthId) {
            throw new RRException("参数有误");
        }
        berthRelateService.remove(new LambdaQueryWrapper<CfgBerthRelate>().eq(CfgBerthRelate::getBerthId, berthId));
        berthRelateService.saveBatch(relates);
    }

}
